﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VSDBM.Code.dto;
using System.Data;
using MySql.Data.MySqlClient;
using VSDBM.Code.dto;
using VSDBM.Code;

/// <summary>
/// Summary description for MatrixDAO...s
/// </summary>
public class MatrixDAO
{
	
    public static void addMatrixTable(String TableName)
    {
        MySqlConnection connection = Connection.getNewConnection();
        try
        {
            try
            {
                MySqlCommand querydelete = new MySqlCommand("DROP TEMPORARY TABLE " + TableName, connection);
                querydelete.ExecuteNonQuery(); 
            }
            catch (System.Exception)
            {
              
            }

            MySqlCommand query = new MySqlCommand("CREATE TEMPORARY TABLE " + TableName + " (posi int, posj int, lastmove int, score float );", connection);
            query.ExecuteNonQuery();
            connection.Close();
        }
        catch (System.Exception)
        {
            connection.Close();
            throw;
        }
    }

    public static void removeMatrixTable(String TableName)
    {
        MySqlConnection connection = Connection.getNewConnection();
        try
        {
            MySqlCommand query = new MySqlCommand("DROP TABLE " + TableName+" ;", connection);
            query.ExecuteNonQuery();
            connection.Close();
        }
        catch (System.Exception)
        {
            connection.Close();
            throw;
        }
    }

    public static Matrix getMatrixTablePosition(String TableName, int posi, int posj)
    {
        MySqlConnection connection = Connection.getNewConnection();
        Matrix mx = new Matrix();
        DataSet ds = new DataSet();
        try
        {
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            adapter.SelectCommand = new MySqlCommand("SELECT * FROM "+TableName+ " WHERE posi="+posi+" AND posj="+posj+" ;", connection);
            adapter.Fill(ds);
            connection.Close();
            mx = Fill(ds, TableName);
            return mx;
        }
        catch (System.Exception)
        {
            connection.Close();
            throw;
        }
    }

    public static Matrix Fill(DataSet ds, String TableName)
    {
        Matrix mx = new Matrix();
        try
        {
            
            mx.posi = Convert.ToInt32(ds.Tables[0].Rows[0]["posi"]);
            mx.posj = Convert.ToInt32(ds.Tables[0].Rows[0]["posj"]);
            mx.lastMove = Convert.ToInt32(ds.Tables[0].Rows[0]["lastmove"]);
            mx.score = (float)ds.Tables[0].Rows[0]["score"];
            mx.table_name = TableName;
        }
        catch (System.Exception)
        {
            
            throw;
        }
       
        return mx;
    }


    public static void updateMatrixTablePosition(String TableName, int posi, int posj, int lastmove, float score)
    {

        MySqlConnection connection = Connection.getNewConnection();
        try
        {
            MySqlCommand query = new MySqlCommand("UPDATE "+TableName+" SET lastmove="+lastmove+",score="+score+" WHERE posi="+posi+" AND posj="+posj, connection);
            query.ExecuteNonQuery();
            connection.Close();
        }
        catch (System.Exception)
        {
            connection.Close();
            throw;
        }



    }

    public static void addMatrixTablePosition(String TableName, int posi, int posj, int lastmove, float score)
    {

        MySqlConnection connection = Connection.getNewConnection();
        try
        {
            MySqlCommand query = new MySqlCommand("INSERT INTO "+ TableName+" (posi,posj,lastmove,score) VALUES ("+posi+","+posj+","+lastmove+","+score+");", connection);
            query.ExecuteNonQuery();
            connection.Close();
        }
        catch (System.Exception)
        {
            connection.Close();
            throw;
        }



    }







}